﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Zxb.DayWork.Core;

namespace Zxb.DayWork.Logic
{
    public class HitDeleteWorker : BatchWorker
    {
        private DateTime minday;

        private static void DeleteHit(DateTime minDate)
        {
            Database db = DB.Home_logAdmin;
            string sql = string.Format("delete from HitsWeiKe_Real where VisitTime<'{0}'", minDate.ToString("yyyy-MM-dd"));
            DbCommand dbCommand = db.GetSqlStringCommand(sql);
            //try
            //{
            //    db.ExecuteNonQuery(dbCommand);
            //}
            //catch(Exception ex)
            //{
            //    Logger.InfoLog.Error(string.Concat("删除点击量出错:", ex.Message));
            //}

            int retry = 5;
            bool flag = false;
            for (int i = 0; i < retry; i++)
            {
                try
                {
                    db.ExecuteNonQuery(dbCommand);
                    flag = true;
                }
                catch (Exception ex)
                {
                    Logger.InfoLog.Info(string.Concat("删除点击量出错:第", i, "次,", ex.Message));
                }
                if (flag)
                {
                    break;
                }
            }
            if (!flag)
            {
                Logger.InfoLog.Error("删除点击量出错:5次都失败");
            }
        }

        public HitDeleteWorker()
        {
            minday = DateTime.Now.AddDays(-54);
        }

        protected override void ReadData()
        {
            
        }

        protected override void ProcessData()
        {

        }

        protected override void WriteData()
        {
            DeleteHit(minday);
        }
    }
}
